﻿Imports Business

Public Class FrmManagerFPTerminal
    Public strServer As String
    Public ktraCboGroup As Integer = 0
    Public Sub New(ByVal serverName As String)
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        strServer = serverName

        '/******************************************************************/
        '/*                  Do du lieu len cboGroup                       */
        '/******************************************************************/
        ktraCboGroup = 1
        Dim bsGroup As New BSGroup(strServer)
        cboGroup.DataSource = bsGroup.EditGetAllGroup()
        cboGroup.DisplayMember = "GROUP_NAME"
        cboGroup.ValueMember = "GROUP_ID"

        '/******************************************************************/
        '/*             Do du lieu len grdFPTerminal                       */
        '/******************************************************************/
        If bsGroup.EditGetAllGroup().Rows.Count <> 1 Then
            Dim bsTerminalSetting As New BSTerminalSetting(strServer)
            cboGroup.SelectedValue = 1
            grdFPTerminal.DataSource = bsTerminalSetting.GetAndCheckAllTerminalSetting(DirectCast(cboGroup.SelectedItem, DataRowView).Item("GROUP_ID"), False)
            SetRowColor()
        End If
    End Sub
    '/******************************************************************/
    '/*       Do du lieu len grdFPTerminal khi chon cboGroup           */
    '/******************************************************************/
    Private Sub cboGroup_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboGroup.SelectedIndexChanged
        ktraCboGroup = ktraCboGroup + 1
        'open frmGroupManager khi chon <<Choose another group>>
        If ktraCboGroup <> 2 Then
            If DirectCast(cboGroup.SelectedItem, DataRowView).Item("GROUP_ID") = 1290 Then
                cboGroup.SelectedIndex = 0
                Dim frmGm As New frmGroupManager(strServer)
                frmGm.ShowDialog()
            Else
                Dim bsTerminalSetting As New BSTerminalSetting(strServer)
                grdFPTerminal.DataSource = bsTerminalSetting.GetAndCheckAllTerminalSetting(DirectCast(cboGroup.SelectedItem, DataRowView).Item("GROUP_ID"), False)
                SetRowColor()
            End If
        End If
    End Sub
    '/******************************************************************/
    '/*                 Delete Terminal Setting                        */
    '/******************************************************************/
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim bsTerminalSetting As New BSTerminalSetting(strServer)
        Dim bsAlarm As New BSAlarm(strServer)
        'Delete record in Terminal Setting table
        If grdFPTerminal.RowCount <> 0 Then
            If MessageBox.Show("Confirm", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) = DialogResult.OK Then
                bsTerminalSetting.DeleteTerminalSetting(grdFPTerminal.CurrentRow.Cells.Item(0).Value)
                bsAlarm.DeleteAlarm(grdFPTerminal.CurrentRow.Cells.Item(0).Value)
            End If
        Else : MsgBox("No", MsgBoxStyle.OkOnly, "")
        End If
        'bsTerminalSetting.DeleteTerminalSetting(bsTerminalSetting.DeleteTerminalSetting(grdFPTerminal.CurrentRow.Cells.Item(0).Value))
        grdFPTerminal.DataSource = bsTerminalSetting.GetAndCheckAllTerminalSetting(DirectCast(cboGroup.SelectedItem, DataRowView).Item("GROUP_ID"), False)
    End Sub
    '/******************************************************************/
    '/*                         Function SetRowColor                   */
    '/******************************************************************/
    Public Sub SetRowColor()
        Dim r As Integer
        For r = 0 To grdFPTerminal.Rows.Count - 1
            If (r + 1) Mod 2 <> 0 Then
                grdFPTerminal.Rows(r).DefaultCellStyle.BackColor = Color.PowderBlue
            Else
                grdFPTerminal.Rows(r).DefaultCellStyle.BackColor = Color.White
            End If
        Next
    End Sub

    '/******************************************************************/
    '/*       Repaint row color Event GrdFPTerminal                    */
    '/******************************************************************/
    Private Sub grdFPTerminal_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles grdFPTerminal.RowPostPaint
        SetRowColor()
    End Sub
    '/******************************************************************/
    '/*                         Click btnNew                           */
    '/******************************************************************/
    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Dim frmFPTerminalDetail As New frmFPTerminalDetail(Nothing, DirectCast(cboGroup.SelectedItem, DataRowView).Item("GROUP_ID"), strServer)
        Dim frmMain As frmMain = Me.ParentForm
        frmMain.AddTabpage(frmFPTerminalDetail, "Detail FP Terminal", "tabDFPT")
    End Sub
    '/******************************************************************/
    '/*                     Double click in record                     */
    '/******************************************************************/
    Private Sub grdFPTerminal_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdFPTerminal.CellDoubleClick
        Dim frmFPTerminalDetail As frmFPTerminalDetail = New frmFPTerminalDetail(grdFPTerminal.CurrentRow, DirectCast(cboGroup.SelectedItem, DataRowView).Item("GROUP_ID"), strServer)
        Dim frmMain As frmMain = Me.ParentForm
        frmMain.AddTabpage(frmFPTerminalDetail, "Detail FP Terminal", "tabDFPT")
    End Sub
    '/******************************************************************/
    '/*                     Click into btnModify                       */
    '/******************************************************************/
    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        Dim frmFPTerminalDetail As frmFPTerminalDetail = New frmFPTerminalDetail(grdFPTerminal.CurrentRow, DirectCast(cboGroup.SelectedItem, DataRowView).Item("GROUP_ID"), strServer)
        Dim frmMain As frmMain = Me.ParentForm
        frmMain.AddTabpage(frmFPTerminalDetail, "Detail FP Terminal", "tabDFPT")
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        frmMain.RemoveTabpage()
    End Sub

    Private Sub btnStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatus.Click
        lblMessage.Text = "Working..."
        Application.DoEvents()
        Dim bsTerminalSetting As New BSTerminalSetting(strServer)
        grdFPTerminal.DataSource = bsTerminalSetting.GetAndCheckAllTerminalSetting(DirectCast(cboGroup.SelectedItem, DataRowView).Item("GROUP_ID"), True)

        ''Test get all user data of FP terminal registration (successfull!)
        'Dim bsFKAttend As New BSFKAttend(strServer)
        'BSFKAttend.SaveUserData()

        'lblMessage.Text = "Save oke"
        'Application.DoEvents()

        'Test get all log data of FP terminal attendance
        Dim bsFKAttend As New BSFKAttend(strServer)
        bsFKAttend.SaveLogData()
        'bsFKAttend.SaveLogData()
        lblMessage.Text = ""
        Application.DoEvents()

    End Sub

    Private Sub cboGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboGroup.Click
        Dim bsGroup As New BSGroup(strServer)
        cboGroup.DataSource = bsGroup.EditGetAllGroup()
        cboGroup.DisplayMember = "GROUP_NAME"
        cboGroup.ValueMember = "GROUP_ID"
    End Sub
End Class